package com.example.mybatisplus.lock.controller;

import com.example.mybatisplus.lock.service.LockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 这里使用程序介绍悲观锁和乐观锁的简单实现
 *
 * @author zhouxl
 * @DATE 2020/9/24
 * @descriptoin
 */
@Controller
@RequestMapping("/api/lock")
public class LockController {

    @Autowired
    private LockService lockService;

    /**
     * 悲观锁
     *
     * @return
     */
    @RequestMapping("/bgLock")
    @ResponseBody
    public Object bgLock(@RequestParam Map<String, Object> map) {
        Map<String, Object> result = lockService.bgLock(map);
        return result;
    }

    /**
     * 乐观锁
     *
     * @return
     */
    @RequestMapping("/lgLock")
    @ResponseBody
    public Object lgLock(@RequestParam Map<String, Object> map) {
        Map<String, Object> result = lockService.lgLock(map);
        return result;
    }

    /**
     * 通过乐观锁实现解析数据流程，避免重复解析数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/lgLockAnaly")
    @ResponseBody
    public Object lgLockAnaly(@RequestParam Map<String, Object> map) {
        Map<String, Object> result = lockService.lgLockAnaly(map);
        return result;
    }

    /**
     * 通过悲观锁实现解析数据方式
     *
     * @param map
     * @return
     */
    @RequestMapping("/bgLockAnaly")
    @ResponseBody
    public Object bgLockAnaly(@RequestParam Map<String, Object> map) {
        return lockService.bgLockAnaly(map);
    }

    /**
     * 测试ab测试工具使用
     *
     * @param
     * @return
     */
    @RequestMapping("/queryUserById")
    @ResponseBody
    public Object queryUserById() {
        return lockService.queryUserById();
    }

}
